package so.shanku.essential.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
import aym.util.getservicesdata.GetServicesDataQueue;
import aym.util.getservicesdata.GetServicesDataUtil;
import aym.util.json.JsonMap;
import aym.util.json.JsonParseHelper;
import net.tsz.afinal.annotation.view.ViewInject;
import so.shanku.essential.R;
import so.shanku.essential.activity.base.BaseUIActivity;
import so.shanku.essential.adapter.RelationProductAdapter;
import so.shanku.essential.getdata.GetDataConfing;
import so.shanku.essential.getdata.GetDataQueue;
import so.shanku.essential.getdata.JsonKeys;
import so.shanku.essential.getdata.ShowGetDataError;
import so.shanku.essential.utils.Constant;
import so.shanku.essential.utils.Utils;
import so.shanku.essential.view.WihteRoundCornersDialog;
import striveen.util.used.view.view.gridview.PLA_AdapterView;
import striveen.util.used.view.view.gridview.XGridView;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2015/9/8.
 */
public class UserCollectionActivity extends BaseUIActivity implements PLA_AdapterView.OnItemLongClickListener, PLA_AdapterView.OnItemClickListener {
	/**
	 * 无相关数据
	 */
	@ViewInject(id = R.id.nodata_layout)
	private LinearLayout nodata_layout;

	@ViewInject(id = R.id.gridview_collection)
	private XGridView gridview_collection;

	private RelationProductAdapter collectionAdapter;
	private List<JsonMap<String, Object>> collectionData;//收藏的数据

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.acitivity_user_collection);
		setCenter_title(R.string.myCollection);
		gridview_collection.setXListViewListener(list_loadMoreListener);
		getData_Collection(true);
		gridview_collection.setOnItemClickListener(this);
		gridview_collection.setOnItemLongClickListener(this);
	}


//	private void getData_deleteCollectionProduct() {
//		Map<String, Object> params = new HashMap<String, Object>();
//		params.put(GetDataQueue.Params_key, GetDataConfing.Key_Str);
//		params.put("Collection", addressId);
//		GetDataQueue queue = new GetDataQueue();
//		queue.setActionName(GetDataConfing.Action_DelMyCollectionProuduct);
//		queue.setWhat(GetDataConfing.What_DelMyCollectionProuduct);
//		queue.setParamsNoJson(params);
//		queue.setCallBack(callBack);
//		getDataUtil.getData(queue);
//	}

	/**
	 * Gridview
	 */
	XGridView.IXListViewListener list_loadMoreListener = new XGridView.IXListViewListener() {
		@Override
		public void onRefresh() {
			getData_Collection(true);
		}

		@Override
		public void onLoadMore() {
			getData_Collection(false);
		}
	};
	/**
	 * 获取网络数据  搜索产品的接口
	 */
	private int page = 0, ismore = 0;

	private void getData_Collection(boolean chearAllData) {
		loadingToast.show();
		if (chearAllData) {
			collectionData = null;
			if (collectionAdapter != null) {
				collectionAdapter.notifyDataSetChanged();
			}
			ismore = 0;
			page = 0;
			gridview_collection.setPullRefreshEnable(true);
			gridview_collection.setPullLoadEnable(true);
		}

		Map<String, Object> params = new HashMap<String, Object>();
		params.put(GetDataQueue.Params_key, GetDataConfing.Key_Str);
		params.put("currentPage", page + 1);
		params.put("pageSize", pageSize);
		params.put("UserName", Utils.getUserAccount(this));
		GetDataQueue queue = new GetDataQueue();
		queue.setCallBack(callBack);
		queue.setActionName(GetDataConfing.GetAction_GetUserAttentionProduct);
		queue.setWhat(GetDataConfing.what_GetUserAttentionProduct);
		queue.setParamsNoJson(params);
		getDataUtil.getData(queue);
	}

	private int pageSize = 10;//每页的个数

	private void setAdatper_singleSortlist(final List<JsonMap<String, Object>> data) {

		if (data.size() == 0 || data.size() % pageSize != 0) {
			ismore = 1;//加载完毕
			gridview_collection.setPullLoadEnable(false);
//			if (page == 0 && data.size() > 0 || page != 0)
//				toast.showToast(R.string.xlistview_no_more_data);

		}
		if (page == 0) {
			this.collectionData = data;
			collectionAdapter = new RelationProductAdapter(this);
			collectionAdapter.setDatas(data);
			collectionAdapter.setShowDiscri(true);
			gridview_collection.setAdapter(collectionAdapter);
		} else {
			this.collectionData.addAll(data);
			collectionAdapter.notifyDataSetChanged();
		}
		page++;

	}

	/**
	 * 获取服务器数据的返回信息
	 */
	private GetServicesDataUtil.IGetServicesDataCallBack callBack = new GetServicesDataUtil.IGetServicesDataCallBack() {
		@Override
		public void onLoaded(GetServicesDataQueue entity) {
			if (entity.isOk()) {

				if (ShowGetDataError.isOkAndCodeIsNot1(UserCollectionActivity.this,
						entity.getInfo())) {
					List<JsonMap<String, Object>> data = JsonParseHelper.getJsonMap_List_JsonMap(
							entity.getInfo(), JsonKeys.Key_Info);
					if (entity.what == GetDataConfing.what_GetUserAttentionProduct) {//我的收藏
						setAdatper_singleSortlist(data);
					}
				} else if (entity.what == GetDataConfing.What_productAddUserAttention) {
					JsonMap<String, Object> data = JsonParseHelper
							.getJsonMap(entity.getInfo());
					try {
						String msg = data.getString(JsonKeys.Key_Msg);
//						toast.showToast(msg);

						updateCollectionStatues();
					} catch (Exception e) {
						e.printStackTrace();
					}

				}
			} else {
				ShowGetDataError.showNetError(UserCollectionActivity.this);

			}
			loadingToast.dismiss();
			invalidateListData();
			gridview_collection.stopLoadMore();
			gridview_collection.stopRefresh();
		}
	};

	private void updateCollectionStatues() {
		 collectionData.remove(itemPosition);
		collectionAdapter.setDatas(collectionData);
		collectionAdapter.notifyDataSetChanged();
	}

	private void invalidateListData() {
		if (collectionData == null || collectionData.size() < 1) {
			gridview_collection.setVisibility(View.GONE);
			nodata_layout.setVisibility(View.VISIBLE);
		} else {
			gridview_collection.setVisibility(View.VISIBLE);
			nodata_layout.setVisibility(View.GONE);
		}
	}

	@Override
	protected void onRestart() {
		super.onRestart();
		getData_Collection(true);
	}

	private String addressId;
	private int itemPosition;
	WihteRoundCornersDialog builder;
	private void showDialog(){

		builder = new WihteRoundCornersDialog(this,
				R.style.ExitDialogStyle, 2, callBackdialog);
		builder.setTitletext(R.string.user_like_cancle_delete_address);
		builder.setMessagetext(R.string.user_like_confirm_cancle_delete_address);
		builder.setButtonText(R.string.cancel, R.string.ensure);
		builder.show();
	}

	private WihteRoundCornersDialog.DialogCallBack callBackdialog = new WihteRoundCornersDialog.DialogCallBack() {

		@Override
		public void bttonclick(int index) {
			builder.dismiss();
			switch (index) {
				case 1:

					break;
				case 2:
					changeGoodsCollect(addressId,callBack);
					break;
				default:
					break;
			}
		}
		@Override
		public void bttonclick(int index, int what) {
		}

	};

	@Override
	public boolean onItemLongClick(PLA_AdapterView<?> parent, View view, int position, long id) {
		itemPosition=position-1;
		addressId=collectionData.get(position - 1).getStringNoNull("ProductId")        ;
		showDialog();
		return false;
	}

	@Override
	public void onItemClick(PLA_AdapterView<?> parent, View view, int position, long id) {
		if(position>0) {
			Intent intent = new Intent(mContext, ProductDetailActivity.class);
			Bundle bundle = new Bundle();
			bundle.putString(Constant.ID, collectionData.get(position - 1).getStringNoNull("ProductId"));
			intent.putExtra(Constant.BUNDLE_EXTRA, bundle);
			startActivity(intent);
		}
	}
}
